package com.nfIt.th03;

import java.util.*;

/**
 * ClossName:MyThreand
 * Pakage:com.nfIt.th03
 * Description
 *
 * @Author L
 * @Create 2024/5/16 19:37
 * @Version 2023.1.3
 * 抽奖线程案列
 */
public class MyThreand implements Runnable {

    List<Integer> list;

    public MyThreand(List<Integer> list) {
        this.list = list;
    }

    @Override
    public void run() {

        while (true) {
            //一个线程执行完另一个线程才能进去
            //所以在里面使用 Thread.sleep(100)：方法没有
            synchronized (MyThreand.class) {
                if (list.size() == 0) {
                    break;
                } else {
                    int index = new Random().nextInt(list.size());
                    System.out.println(Thread.currentThread().getName() + "又产生了一个" + list.get(index) + "元大奖。");
                    list.remove(index);
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
